import java.util.ArrayList;
import java.util.List;

class Solution {
    public int n;
    public int[] tree;
    public List<Integer> countOfPeaks(int[] nums, int[][] queries) {
        List<Integer> ans=new ArrayList<>();

        n= nums.length;
        tree=new int[n+1];

        for (int i = 1; i < n-1; i++) {
            if(nums[i]>nums[i-1] && nums[i]>nums[i+1]){
                update(i,1);
            }
        }

        for (int i = 0; i < queries.length; i++) {
            if(queries[i][0]==1){
                //获取峰值数目
                if(queries[i][2]<queries[i][1]+2){
                    ans.add(0);
                }else{
                    ans.add(count(queries[i][2]-1)-count(queries[i][1]));
                }
            }else{
                //更新数目
                int index=queries[i][1];
                int val=queries[i][2];
                //先将被改变位置的区间全改一遍
                for (int j = Math.max(index-1,1); j <= Math.min(index+1,n-2); j++) {
                    if(nums[j]>nums[j-1] && nums[j]>nums[j+1]){
                        update(j,-1);
                    }
                }
                nums[index]=val;
                for (int j = Math.max(index-1,1); j <= Math.min(index+1,n-2); j++) {
                    if(nums[j]>nums[j-1] && nums[j]>nums[j+1]){
                        update(j,1);
                    }
                }
            }
        }

        return ans;
    }

    public void update(int index,int val){
        for (int i = index+1; i < n; i+=i&(-i)) {
            tree[i]+=val;
        }
    }

    public int count(int p){
        int result=0;
        while(p!=0){
            result+=tree[p];
            p-=p&(-p);
        }
        return result;
    }
}